package com.tangdi.mkfile.bank_bankcc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_ZGGZMX;
import com.tangdi.Dao.impl.GongziBankInfoImpl;
import com.tangdi.mkfile.gzdwhz.CreateDWGZExcel_LISTALL;


/**
 * @author yang
 * 
 */
public class CreateBankCCTxt {
	private Logger logger = Logger.getLogger(CreateDWGZExcel_LISTALL.class);
	 public String file_url = Utils.getFilePath("root_path");
	 private String file_Type = "txt";
	 private DecimalFormat decimalFormat=new DecimalFormat("#0.00");
	 
	 public int writeFile(String month){
		 
		 String filePath = file_url  + month + File.separator + file_Type + File.separator + month + "_bankcc.txt";
	        try {
	            File file = new File(filePath);
	            String tab = "\t\t";
	            String enter = "\r\n";
	            String blank = "     ";
	            if(!file.exists()){
	            	file.getParentFile().mkdirs();
	            	file.createNewFile();
	   			}
	            
	            FileWriter writer = new FileWriter(file);
	            StringBuffer write_str = new StringBuffer(); 
	            BufferedWriter out = new BufferedWriter(writer);
	            GongziBankInfoImpl gzbank = new GongziBankInfoImpl();
	            
/*	            	差错核对表
	            	实际代发金额=总金额-电汇金额-无账号金额
	            	实际代发笔数=成功总笔数-电汇笔数+实发为0笔数
	            	金额不为0，且交易状态不为S的均为代发失败
     			select * from CZGZ_ZGGZMX where TXN_AMT!='0' and DTL_STS!='S' and yf='201906'
*/	            
	            String sf_amt = gzbank.getGZ_TOTAL(month, " AND DTL_STS = 'S'");
	            String sf_count = gzbank.getGZ_COUNT(month, " AND DTL_STS = 'S'");
	            
	            String wzh_amt = gzbank.getGZ_TOTAL(month, " AND (PER_AC = '' OR PER_AC = null)");
	            String wzh_count = gzbank.getGZ_COUNT(month, " AND (PER_AC = '' OR PER_AC = null)");
	            
	            String sf0_count = gzbank.getGZ_COUNT(month, " AND (TXN_AMT = '' OR TXN_AMT = '0.00' OR TXN_AMT = null)");
	            
	            String dh_amt = gzbank.getGZ_TOTAL(month, " AND PER_AC = '2'");
	            String dh_count = gzbank.getGZ_COUNT(month, " AND PER_AC = '2'");

	            int sf_count_js = Integer.valueOf(sf_count) + Integer.valueOf(sf0_count) - Integer.valueOf(dh_count);
	            logger.info("sf_amt: " + sf_amt + " | " + "sf_count: " + sf_count + " | " + 
	            			"wzh_amt: " + wzh_amt + " | " +  "wzh_count: " + wzh_count + " | " + 
	            		    "sf0_count: " + sf0_count + " | " +  "sf_count_js: " + sf_count_js);
	            
                write_str.append(enter);                
                write_str.append(tab + tab + "交通银行南京分行  市财政代发工资差错核对表");
                write_str.append(enter);
                write_str.append(enter);
                
                write_str.append(blank + "年月：" + month);
                write_str.append(enter);
                
                Date sys_date = new Date(); 
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");//可以方便地修改日期格式
                write_str.append(blank + "打印日期：" + dateFormat.format(sys_date));
                write_str.append(enter);
                write_str.append(enter);
                
                write_str.append(blank + appentStr4Length("实际代发",10,""));
                write_str.append("笔数：");
                write_str.append(appentStr4Length(sf_count_js + "", 8, "r"));
                write_str.append(blank + "金额：");
                write_str.append(appentStr4Length(decimalFormat.format(Double.parseDouble(sf_amt) - Double.parseDouble(dh_amt) - Double.parseDouble(wzh_amt)) + "", 15, "r"));
                write_str.append(enter);
                
                write_str.append(blank + appentStr4Length("无账号",10,""));
                write_str.append("笔数：");
                write_str.append(appentStr4Length(wzh_count, 8, "r"));
                write_str.append(blank + "金额：");
                write_str.append(appentStr4Length(wzh_amt, 15, "r"));
                write_str.append(enter);
                
                write_str.append(blank + appentStr4Length("实发为0",10,""));
                write_str.append("笔数：");
                write_str.append(appentStr4Length(sf0_count, 8, "r"));
                write_str.append(enter);
                
                out.write(write_str.toString());
                out.write(blank + "----------------------------------------------------------------------------------------------------");
                out.write(enter);
                
                out.write(blank + "错误原因：银行账号有误明细");
                out.write(enter);
                
                List<CZGZ_ZGGZMX>  list_error = new ArrayList<CZGZ_ZGGZMX>();
                list_error = gzbank.getGZ_ERROR(month);
                for(int i = 0; i < list_error.size(); i++){
                	CZGZ_ZGGZMX mx = new CZGZ_ZGGZMX();
                	mx = list_error.get(i);
                	out.write(blank + appentStr4Length(mx.getDW_MC(), 50, ""));
                	out.write(appentStr4Length(mx.getNME(), 10, ""));
                	out.write(appentStr4Length(mx.getPER_AC(), 20, ""));
                	out.write(appentStr4Length(mx.getSF_TXN_AMT(), 15, "r"));
                	out.write(enter);
                }
                
                out.write(blank + "----------------------------------------------------------------------------------------------------");
                out.write(enter);
                
                out.flush();
                out.close();
	            
	        } catch (IOException e) {
	            e.printStackTrace();
	            return -1;
	        }
	        return 0;
	    }

    public static String appentStr4Length(String str , int length, String align){
        if(str == null){
            str = "";
        }
        try {
            int strLen = 0;//计算原字符串所占长度,规定中文占两个,其他占一个
            for(int i = 0 ; i<str.length(); i++){
                if(isChinese(str.charAt(i))){
                    strLen = strLen + 2;
                }else{
                    strLen = strLen + 1;
                }
            }
            if(strLen>=length){
                return str;
            }
            int remain = length - strLen;//计算所需补充空格长度
            if("r".equals(align)){
            	for(int i =0 ; i< remain ;i++){
            		str = " " + str;
            	}
            }else{
            	for(int i =0 ; i< remain ;i++){
            		str = str + " ";
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
    // 根据Unicode编码完美的判断中文汉字和符号
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }
	    
/*	public static void main(String[] args) {
		CreateBankCCTxt cb = new CreateBankCCTxt();
		String month = "201905";
		cb.writeFile(month);

	}*/

}
